<template>
  <div class="app-container">
    <el-card>
      <el-card class="operate-container" shadow="never">
        <i class="el-icon-tickets"></i>
        <span>数据列表</span>
        <el-button size="mini" class="btn-add" @click="handleAdd()">添加视频</el-button>
      </el-card>
      <div class="table-container">
        <el-table
          ref="homeAdvertiseTable"
          :data="list"
          style="width: 100%;"
          @selection-change="handleSelectionChange"
          v-loading="listLoading"
          border>
          <el-table-column label="视频名称" align="center">
            <template slot-scope="scope">{{ scope.row.videoName }}</template>
          </el-table-column>
          <el-table-column label="时长" align="center">
            <template slot-scope="scope">{{ scope.row.videoTime }}</template>
          </el-table-column>
          <el-table-column label="价格" align="center">
            <template slot-scope="scope">{{ scope.row.price }}</template>
          </el-table-column>
          <el-table-column label="售课方式" align="center">
            <template slot-scope="scope">{{ scope.row.priceTypeName }}</template>
          </el-table-column>
          <el-table-column label="播放次数" align="center">
            <template slot-scope="scope">{{ scope.row.playCount }}</template>
          </el-table-column>
          <el-table-column label="上传时间" width="160" align="center">
            <template slot-scope="scope">{{ scope.row.updateTime | formatDateTime }}</template>
          </el-table-column>
          <el-table-column label="操作" width="120" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container" style="margin-bottom: 20px">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.pageSize"
          :page-sizes="[5,10,15]"
          :current-page.sync="listQuery.pageNum"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-row type="flex" justify="end" style="margin-top: 20px">
      <el-col :span="2">
        <el-button @click="back">返回</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { curVideoList, curVideoDel } from '@/api/course'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null
    }
  },
  created () {
    this.listQuery.sourceId = this.$route.query.id
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteVideo(row.id)
    },
    handleAdd () {
      this.$router.push({ path: '/course-ms/addVideo', query: { id: this.$route.query.id } })
    },
    handleUpdate (index, row) {
      this.$router.push({ path: '/course-ms/updateVideo', query: { id: row.id } })
    },
    getList () {
      this.listLoading = true
      curVideoList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    deleteVideo (id) {
      this.$confirm('是否要删除该视频?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        curVideoDel({ id, courseId: this.$route.query.id }).then(response => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    back () {
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
</style>
